package org.goj.practice.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.goj.common.entity.practice.Classify;
import org.goj.practice.mapper.ClassifyMapper;
import org.goj.practice.service.ClassifyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.goj.practice.vo.ClassifyCascaderVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 题目分类 服务实现类
 * </p>
 *
 * @author CK
 * @since 2020-12-07
 */
@Service
public class ClassifyServiceImpl extends ServiceImpl<ClassifyMapper, Classify> implements ClassifyService {

    @Resource
    private ClassifyMapper classifyMapper;

    @Override
    public List<ClassifyCascaderVO> listClassifyCascader() {
        List<Classify> classifies = super.list();
        List<ClassifyCascaderVO> cascaderVOList1 = new ArrayList<>();
        for (Classify classify1 : classifies) {
            if (classify1.getClassifyParentId() == 0) {
                ClassifyCascaderVO classifyCascaderVO1 = new ClassifyCascaderVO();
                classifyCascaderVO1.setValue(classify1.getClassifyId());
                classifyCascaderVO1.setLabel(classify1.getClassifyName());
                List<ClassifyCascaderVO> cascaderVOList2 = new ArrayList<>();
                for (Classify classify2 : classifies) {
                    if (classifyCascaderVO1.getValue().equals(classify2.getClassifyParentId())) {
                        ClassifyCascaderVO classifyCascaderVO2 = new ClassifyCascaderVO();
                        classifyCascaderVO2.setValue(classify2.getClassifyId());
                        classifyCascaderVO2.setLabel(classify2.getClassifyName());
                        List<ClassifyCascaderVO> cascaderVOList3 = new ArrayList<>();
                        for (Classify classify3 : classifies) {
                            if (classifyCascaderVO2.getValue().equals(classify3.getClassifyParentId())) {
                                ClassifyCascaderVO classifyCascaderVO3 = new ClassifyCascaderVO();
                                classifyCascaderVO3.setValue(classify3.getClassifyId());
                                classifyCascaderVO3.setLabel(classify3.getClassifyName());
                                classifyCascaderVO3.setChildren(null);
                                cascaderVOList3.add(classifyCascaderVO3);
                            }
                        }
                        classifyCascaderVO2.setChildren(cascaderVOList3);
                        cascaderVOList2.add(classifyCascaderVO2);
                    }
                }
                classifyCascaderVO1.setChildren(cascaderVOList2);
                cascaderVOList1.add(classifyCascaderVO1);
            }
        }
        return cascaderVOList1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertClassify(Integer classifyParentId, String classifyName) {
        QueryWrapper<Classify> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("classify_id").eq("classify_parent_id", classifyParentId).ne("classify_id", 9);
        List<Classify> list = classifyMapper.selectList(queryWrapper);
        if (classifyParentId == 0) {
            Assert.isTrue(list.size() < 8, "一级分类已满");
        }

        int sequence = classifyParentId * 1000 + list.size() + 1;
        for (int i = classifyParentId * 1000 + 1; i <= classifyParentId * 1000 + list.size() + 1; i++) {
            int finalI = i;
            if (list.stream().noneMatch(item -> item.getClassifyId().equals(finalI))) {
                sequence = i;
            }
        }

        Classify classify = new Classify();
        classify.setClassifyId(sequence).setClassifyName(classifyName).setClassifyParentId(classifyParentId);
        int count = classifyMapper.insert(classify);
        Assert.isTrue(count == 1, "添加分类失败");
    }
}
